window.onload=function () {
	game=new Game();
}

// var width=ctx.width;
// var height=ctx.height;

Game=function(){
	//game state
	this.STATE_RUN=0;
	this.STATE_PAUSE=1;
	this.STATE_STOP=2;
	//directions
	this.DIR_UP=3;
	this.DIR_DOWN=4;
	this.DIR_LEFT=5;
	this.DIR_RIGHT=6;

	this.cellWidth=20;
	this.cellHeight=20;

	this.canvas=document.getElementById('doit');
	this.ctx=this.canvas.getContext('2d');

	this.init();
}

Game.prototype.init=function(){
	console.log('init...');
	this.clear();

	this.snake=[]
	this.carrot=this.createCarrot();

	this.currentState=this.STATE_RUN;
	this.currentDirection=this.DIR_RIGHT;

	//first part of the snake
	this.snake.push(new Game.Block(0,0,this.cellWidth,this.cellHeight,'#000000'));

	// this.carrot=this.createCarrot();

	this.draw();
	this.control();
	this.start();
}

Game.prototype.start=function(){
	var game=this;
	var snakeHead;
	var t=setInterval(function(){
		game.clear();
		switch(game.currentDirection){
			case game.DIR_UP:
				snakeHead=new Game.Block(game.snake[0].x,
					game.snake[0].y-game.cellHeight,
					game.cellWidth,
					game.cellHeight,
					game.snake[0].color);
				break;
			case game.DIR_DOWN:
				snakeHead=new Game.Block(game.snake[0].x,
					game.snake[0].y+game.cellHeight,
					game.cellWidth,
					game.cellHeight,
					game.snake[0].color);
				break;
			case game.DIR_LEFT:
				snakeHead=new Game.Block(game.snake[0].x-game.cellWidth,
					game.snake[0].y,
					game.cellWidth,
					game.cellHeight,
					game.snake[0].color);
				break;
			case game.DIR_RIGHT:
				snakeHead=new Game.Block(game.snake[0].x+game.cellWidth,
					game.snake[0].y,
					game.cellWidth,
					game.cellHeight,
					game.snake[0].color);
				break;
		}
		if(game.gameOver()){
			clearInterval(t);
			// alert('GAME OVER.');
			game.clear();
			new Game();
			return;
			// game.init();
		}
		var length;
		if(snakeHead.x==game.carrot.x &&
			snakeHead.y==game.carrot.y){
			//touch carrot, eat it
			length=game.snake.length;
			game.carrot=game.createCarrot();
		}else{
			length=game.snake.length-1;
		}

		var newSnake=[snakeHead];
		for (var i=0;i<length;i++) {
			newSnake.push(game.snake[i]);
		};
		game.snake=newSnake;
		game.draw();
	},500);
}

Game.prototype.draw=function(){
	//draw snake
	for (var i = this.snake.length - 1; i >= 0; i--) {
		this.ctx.fillStyle=this.snake[i].color;
		this.ctx.fillRect(this.snake[i].x,this.snake[i].y,this.snake[i].width,this.snake[i].height,this.snake[i].color);
	};
	//draw carrot
	this.ctx.fillStyle=this.carrot.color;
	this.ctx.fillRect(this.carrot.x,this.carrot.y,this.carrot.width,this.carrot.height,this.carrot.color);
}

Game.prototype.clear=function(){
	this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
}

Game.prototype.control=function (){
	//listen to key event, control the game
	var game=this;
	window.addEventListener('keydown',function(e){
		switch(e.keyCode){
			case 37://left
				if(game.currentDirection!=game.DIR_RIGHT){
					game.currentDirection=game.DIR_LEFT;
				}
				break;
			case 38://up
				if(game.currentDirection!=game.DIR_DOWN){
					game.currentDirection=game.DIR_UP;
				}
				break;
			case 39://right
				if(game.currentDirection!=game.DIR_LEFT){
					game.currentDirection=game.DIR_RIGHT;
				}
				break;
			case 40://down
				if(game.currentDirection!=game.DIR_UP){
					game.currentDirection=game.DIR_DOWN;
				}
				break;
			case 32://space
				game.pause();
		}
	},true);
	window.focus();
}

Game.prototype.pause=function () {
	//pause game
	if(this.currentState==this.STATE_RUN){
		this.currentState=this.STATE_PAUSE;
	}else{
		this.currentState=this.STATE_RUN;
	}
}

Game.prototype.createCarrot=function(){
	//make up a new carrot
	var x=Math.floor(this.canvas.width/this.cellWidth*Math.random())*this.cellWidth;
	var y=Math.floor(this.canvas.height/this.cellHeight*Math.random())*this.cellHeight;
	var color='#FF0000';
	return new Game.Block(x,y,this.cellWidth,this.cellHeight,color);
}

Game.prototype.gameOver=function(){
	var snakeHead=this.snake[0];
	if(snakeHead.x<0 ||
		snakeHead.y<0 ||
		snakeHead.x>this.canvas.width-this.cellWidth ||
		snakeHead.y>this.canvas.height-this.cellHeight){
		//touch wall, game is over
		return true;
	}
	for(var i=1;i<this.snake.length;i++){
		if(this.snake[i].x==snakeHead.x && 
			this.snake[i].y==snakeHead.y){
			//touch itself, game is over
			return true;
		}
	}
	return false;
}